﻿using System.Collections;
using System.Windows.Forms;
using System.Drawing;

namespace LuaCEGUI.Controls
{
    public static class ControlManager
    {
        static Hashtable ControlName = new Hashtable(); //key:Name Value:ID
        static Hashtable ControlType = new Hashtable(); //key:ID    Value:Type(string)
        static Hashtable ControlHandler = new Hashtable();  //key:ID    Value:ControlObject
        static Hashtable ControlMethon = new Hashtable();   //key:ID    Value:Methon
        static int ControlCount = 0;

        public static int AddControl(string name, string type)
        {
        /* **********
         * Add a control to ControlList
         ********** */
            int h = -1;
            switch (type)
            {
                case "Form":
                    NormalForm f = new NormalForm(name);
                    ControlName.Add(name, ControlCount);
                    ControlType.Add(ControlCount, "Form");
                    ControlHandler.Add(ControlCount, f);
                    h = ControlCount;
                    ControlCount++;
                    break;
                case "Button":
                    NormalButton b = new NormalButton(name);
                    ControlName.Add(name, ControlCount);
                    ControlType.Add(ControlCount, "Button");
                    ControlHandler.Add(ControlCount, b);
                    h = ControlCount;
                    ControlCount++;
                    break;
                case "Label":
                    NormalLabel l = new NormalLabel(name);
                    ControlName.Add(name, ControlCount);
                    ControlType.Add(ControlCount, "Label");
                    ControlHandler.Add(ControlCount, l);
                    h = ControlCount;
                    ControlCount++;
                    break;
                case "TextBox":
                    NormalTextBox t = new NormalTextBox(name);
                    ControlName.Add(name, ControlCount);
                    ControlType.Add(ControlCount, "TextBox");
                    ControlHandler.Add(ControlCount, t);
                    h = ControlCount;
                    ControlCount++;
                    break;

                default:
                    h = -1;
                    break;
            }
            return h;
        }

        public static Control GetHandlerByID(int id)    //Return instance of the class!
        {
        /* **********
        * Get a instance
        ********** */
            if (ControlHandler.Contains(id))
            {
                return (Control)ControlHandler[id];
            }
            return null;
        }
        public static int GetIDByName(string name)
        {
            if (ControlName.Contains(name))
            {
                return (int)ControlName[name];
            }
            return -1;
        }

        private static int SetEventHandler(int id, string methon)
        {
        /* **********
        * Add a EventHandler to a Control
        ********** */
            string type = GetTypeByID(id);
            switch (type)
            {
                case "Button":
                    ControlMethon[id] = methon;
                    NormalButton b = (NormalButton)ControlHandler[id];
                    if (b == null) return 0;
                    b.Click += (EventHandler.Button_Click);
                    break;

            }
            return 1;
        }
        public static int SetEventHandlerByName(string name, string methon)
        {
            int id = GetIDByName(name);
            return SetEventHandler(id, methon);
        }
        public static string GetEventHandler(int id)
        {
        /* **********
        * Get the EventHandler of a Control
        ********** */
            if (ControlMethon.Contains(id))
            {
                return (string)ControlMethon[id];
            }
            return null;
        }

        private static string GetTypeByID(int id)
        {
            if (ControlType.Contains(id))
            {
                return (string)ControlType[id];
            }
            return null;
        }
        public static string GetTypeByName(string name)
        {
            int id = GetIDByName(name);
            return GetTypeByID(id);
        }

        private static int AddControlToContainer(int container,int control)
        {
            string Type_Container = GetTypeByID(container);
            string Type_Control = GetTypeByID(control);
            
            switch (Type_Container)
            {
                case "Form":
                    NormalForm f = (NormalForm)GetHandlerByID(container);
                    if (f == null) return 0;
                    f.Controls.Add(GetHandlerByID(control));
                    break;
            }

            return 1;
        }
        public static int AddControlToContainerByName(string container, string control)
        {
            int id_container = GetIDByName(container);
            int id_control = GetIDByName(control);
            return AddControlToContainer(id_container, id_control);
        }

        private static int RunForm(int fid)
        {
            NormalForm f = (NormalForm)GetHandlerByID(fid);
            if (f == null) return 0;
            Application.Run(f);
            return 1;
        }

        public static int RunFormByName(string name)
        {
            int id = GetIDByName(name);
            return RunForm(id);
        }

        private static int SetControlText(int id, string text)
        {
            Control c = GetHandlerByID(id);
            if (c == null) return 0;
            c.Text = text;
            return 1;
        }
        public static int SetControlTextByName(string name, string text)
        {
            int id = GetIDByName(name);
            return SetControlText(id, text);
        }

        private static int SetControlLocation(int id,int x, int y)
        {
            Point p = new Point(x, y);
            Control c = GetHandlerByID(id);
            c.Location = p;
            return 1;
        }
        public static int SetControlLocationByName(string name, int x, int y)
        {
            int id = GetIDByName(name);
            return SetControlLocation(id,x,y);
        }
        public static int SetControlSize(int id, int width, int height)
        {
            Size s = new Size(width, height);
            Control c = GetHandlerByID(id);
            c.Size = s;
            return 1;
        }
        public static int SetControlSizeByName(string name, int width, int height)
        {
            int id = GetIDByName(name);
            return SetControlSize(id, width, height);
        }
        public static string GetControlText(int id)
        {
            Control c = GetHandlerByID(id);
            return c.Text;
        }
        public static string GetControlTextByName(string name)
        {
            int id = GetIDByName(name);
            return GetControlText(id);
        }
    }
}
